/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2019-07-28     Ernest         the first version
 * 2024-09-08     SliverWang     modify
 */

#ifndef __DRV_SOUND_H_
#define __DRV_SOUND_H_

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#include "drv_dma.h"
#include "drv_common.h"

#define CODEC_I2C_NAME                  ("hwi2c1")
#define AUDIO_TX_Handle                  SPI2
#define AUDIO_RX_Handle                  I2S2EXT

/* DMA1 channel3 */
#define AUDIO_RX_DMA_IRQHandler          DMA1_Channel3_IRQHandler
#define AUDIO_RX_DMA_CLOCK               CRM_DMA1_PERIPH_CLOCK
#define AUDIO_RX_DMA_CHANNEL             DMA1_CHANNEL3
#define AUDIO_RX_DMA_IRQ                 DMA1_Channel3_IRQn
#define AUDIO_RX_DMA_MUX_CHANNEL         DMA1MUX_CHANNEL3
#define AUDIO_RX_DMA_REQ_ID              DMAMUX_DMAREQ_ID_I2S2_EXT_RX
#define AUDIO_RX_DMA_HDT_FLAG            DMA1_HDT3_FLAG
#define AUDIO_RX_DMA_FDT_FLAG            DMA1_FDT3_FLAG
#define AUDIO_RX_DMA_GL_FLAG             DMA1_GL3_FLAG

/* DMA1 channel4 */
#define AUDIO_TX_DMA_IRQHandler          DMA1_Channel4_IRQHandler
#define AUDIO_TX_DMA_CLOCK               CRM_DMA1_PERIPH_CLOCK
#define AUDIO_TX_DMA_CHANNEL             DMA1_CHANNEL4
#define AUDIO_TX_DMA_IRQ                 DMA1_Channel4_IRQn
#define AUDIO_TX_DMA_MUX_CHANNEL         DMA1MUX_CHANNEL4
#define AUDIO_TX_DMA_REQ_ID              DMAMUX_DMAREQ_ID_SPI2_TX
#define AUDIO_TX_DMA_HDT_FLAG            DMA1_HDT4_FLAG
#define AUDIO_TX_DMA_FDT_FLAG            DMA1_FDT4_FLAG
#define AUDIO_TX_DMA_GL_FLAG             DMA1_GL4_FLAG

#define AUDIO_FREQUENCY_016K            ((rt_uint32_t) 16000)
#define AUDIO_FREQUENCY_08K             ((rt_uint32_t) 8000)

#define DRV_I2S_BUFF_DEFAULT_SIZE       (AUDIO_FREQUENCY_016K / 1000 * 20 * 2 * 2 * 2)

typedef struct
{
    struct rt_audio_device audio;
    struct rt_audio_configure audio_config;

    uint16_t tx_size;
    uint16_t rx_size;

    rt_uint8_t* tx_buff;
    rt_uint8_t* rx_buff;

    struct rt_i2c_bus_device *i2c_bus;

    rt_uint8_t is_running;
} drv_i2s_t;


#endif
